//
//  MemberDetailViewController.m
//  location
//
//  Created by Dongxiang Xu on 2025/5/8.
//

#import "MemberDetailViewController.h"
#import "AddMemberNotesView.h"
#import "UIImageView+WebCache.h"

@interface MemberDetailViewController ()

@end

@implementation MemberDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = WhiteColor;
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 74, 44)];
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = RealBlackColor;
    title.font = MEDIUM_FONT(18);
    title.text = @"成员信息";
    self.navigationItem.titleView = title ;
    
    [self initView];
}

- (void)initView
{
    UIImageView *header = [[UIImageView alloc] initWithFrame:CGRectMake(16, NAVHEIGHT+22, 60, 60)];
    header.layer.masksToBounds = YES;
    [header.layer setCornerRadius:16];
    header.backgroundColor = RealWhiteColor;
    [header sd_setImageWithURL:[NSURL URLWithString:_userInfo[@"avatar"]]];
    [self.view addSubview:header];
    
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(header.maxX+8, NAVHEIGHT+27, SCREEN_WIDTH-header.x-24, 22)];
    name.font = REAL_MEDIUM_FONT(17);
    name.textColor = RealBlackColor;
    name.text = [NSString stringWithFormat:@"%@",_userInfo[@"nickName"]];
    [self.view addSubview:name];
        
    UILabel *invitInfo = [[UILabel alloc] initWithFrame:CGRectMake(header.maxX+8, name.maxY, name.width, 22)];
    invitInfo.font = DEFAULT_FONT(13);
    invitInfo.textColor = GraryLightColor;
    invitInfo.text = [NSString stringWithFormat:@"%@ 由%@邀-请加入家庭",[_userInfo[@"joinTime"] substringToIndex:10],_userInfo[@"invitedBy"]];
    [self.view addSubview:invitInfo];
        
    UIView *infoView = [[UIView alloc] initWithFrame:CGRectMake(16, header.maxY+22, SCREEN_WIDTH-32, 96)];
    infoView.backgroundColor = RealWhiteColor;
    infoView.layer.masksToBounds = YES;
    [infoView.layer setCornerRadius:16];
    [self.view addSubview:infoView];
    
    NSArray *infoArray = @[@{@"title":@"账号",@"info":_userInfo[@"phonenumber"]},@{@"title":@"备注",@"info":_userInfo[@"remark"]}];
    for (int i = 0; i < infoArray.count; i ++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 48*i, infoView.width, 48)];
        [infoView addSubview:button];
        
        UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, 32, button.height)];
        name.text = infoArray[i][@"title"];
        name.font = REAL_MEDIUM_FONT(15);
        name.textColor = RealBlackColor;
        [button addSubview:name];
        
        UILabel *info = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(name.frame)+10, 0, button.width-CGRectGetMaxX(name.frame)-56, button.height)];
        info.textAlignment = NSTextAlignmentRight;
        info.text = infoArray[i][@"info"];
        info.font = DEFAULT_FONT(14 );
        info.textColor = GraryTooHeavyColor;
        [button addSubview:info];
        
        if (i==0) {
            info.width = button.width-CGRectGetMaxX(name.frame)-28;
            
            UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(20, button.height-1, infoView.width-20, 1)];
            line.image = [UIImage imageNamed:@"line"];
            [button addSubview:line];
        }else{
            if (self.adminId.integerValue == [XDXShareInstance shareInstance].userId.integerValue || [self.userInfo[@"userId"] integerValue]==[XDXShareInstance shareInstance].userId.integerValue) {
                UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(button.width-40, (button.height-12)/2, 12, 12)];
                arrow.image = [UIImage imageNamed:@"arrow_right"];
                [button addSubview:arrow];
            }else{
                info.width = button.width-CGRectGetMaxX(name.frame)-28;
            }
        }
        
        [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (i==0) {
                
            }else if (i==1){
                if (self.adminId.integerValue == [XDXShareInstance shareInstance].userId.integerValue || [self.userInfo[@"userId"] integerValue]==[XDXShareInstance shareInstance].userId.integerValue) {
                    AddMemberNotesView *view = [[AddMemberNotesView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) UserInfo:self.userInfo];
                    [self.view addSubview:view];
                    view.AddMemberNotesBlock = ^(NSString *reamrk) {
                        info.text = reamrk;
                        if (self.MemberDetailRemarkBlock) {
                            self.MemberDetailRemarkBlock(reamrk);
                        }
                    };
                }               
            }
        }];
    }
    
    if (self.adminId.integerValue == [XDXShareInstance shareInstance].userId.integerValue && [self.userInfo[@"userId"] integerValue]!=[XDXShareInstance shareInstance].userId.integerValue) {
        UIButton *delete = [[UIButton alloc] initWithFrame:CGRectMake(16, SCREEN_HEIGHT-100, SCREEN_WIDTH-32, 60)];
        delete.layer.masksToBounds = YES;
        [delete.layer setCornerRadius:20];
        delete.backgroundColor = [GraryHeavyLightColor colorWithAlphaComponent:0.3];
        [delete setTitle:@" 移除成员" forState:UIControlStateNormal];
        [delete setTitleColor:RealWhiteColor forState:UIControlStateNormal];
        [delete.titleLabel setFont:MEDIUM_FONT(16)];
        [delete setImage:[UIImage imageNamed:@"delete_member"] forState:UIControlStateNormal];
        [self.view addSubview:delete];
        [[delete rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"确认移除该成员吗？"
                                                                        message:nil
                                                                 preferredStyle:UIAlertControllerStyleActionSheet];
            // 加粗标题
            NSMutableAttributedString *attrTitle = [[NSMutableAttributedString alloc] initWithString:@"确认移除该成员吗？"];
            [attrTitle addAttribute:NSFontAttributeName value:MEDIUM_FONT(17) range:NSMakeRange(0, attrTitle.length)];
            [ac setValue:attrTitle forKey:@"attributedTitle"];
            [ac addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                NSDictionary *param = [NSMutableDictionary dictionary];
                [param setValue:self.userInfo[@"userId"] forKey:@"userId"];
                [param setValue:self.userInfo[@"id"] forKey:@"groupId"];
                CryptoHTTPKit *manager = [[CryptoHTTPKit alloc] init];
                [manager encryptedPOST:@"/system/group/removeusers"
                parameters:param
                success:^(NSDictionary *responseObject) {
                    if ([responseObject[@"code"] integerValue]==200) {
                        [self.navigationController popViewControllerAnimated:YES];
                        [XDXShareInstance shareInstance].ReloadGroupListFlag = YES;
                        if (self.MemberDetailDeleteBlock) {
                            self.MemberDetailDeleteBlock();
                        }
                    }
                    [[HCToast shareInstance] showToast:responseObject[@"msg"]];
                }
                failure:^(NSError *aError) {
                    
                }];
            }]];
            [ac addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
            UIPopoverPresentationController *pop = ac.popoverPresentationController;
            pop.sourceView = delete;
            pop.sourceRect = delete.bounds;
            // 去掉气泡箭头
            pop.permittedArrowDirections = 0; // 等价于无方向，隐藏箭头
            [self presentViewController:ac animated:YES completion:nil];
        }];
    }    
}

@end
